package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.pojo.ProjectNum;
import com.qingjiang.projectschedule.service.ProjectNumService;
import com.qingjiang.projectschedule.mapper.ProjectNumMapper;
import com.qingjiang.projectschedule.utils.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author jx
* @description 针对表【project_num】的数据库操作Service实现
* @createDate 2024-03-18 11:03:36
*/
@Service
public class ProjectNumServiceImpl extends ServiceImpl<ProjectNumMapper, ProjectNum>
    implements ProjectNumService{
    @Autowired
    private ProjectNumMapper numMapper;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");

    @Override
    public String generateProjectId() {
        String date = dateFormat.format(new Date());
        LambdaQueryWrapper<ProjectNum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectNum::getCurrentMonth,date);
        ProjectNum projectNum = numMapper.selectOne(queryWrapper);
        if (projectNum == null) {
            ProjectNum projectNum_new = new ProjectNum(date,2);
            int rows =  numMapper.insert(projectNum_new);
            return date+"-"+MyStringUtil.generateCount(1);
        }
        Integer num = projectNum.getNum();
        String projectId = date+"-"+MyStringUtil.generateCount(num);
        projectNum.setNum(num+1);
        numMapper.updateById(projectNum);
        return projectId;
    }
}




